package net.maku.express.biza.service.orderlist.impl;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.maku.commons.biza.handle.CreateSysNOHandler;
import net.maku.express.biza.controller.orderlist.vo.*;
import net.maku.express.biza.convert.OrderListConvert;
import net.maku.express.biza.mapper.collectexpress.CollectExpressMapper;
import net.maku.express.biza.mapper.collectexpress.CollectExpressSmallMapper;
import net.maku.express.biza.mapper.orderlist.ChargeDetailsInfoMapper;
import net.maku.express.biza.mapper.orderlist.OrderListAnnexsMapper;
import net.maku.express.biza.mapper.orderlist.OrderListMapper;
import net.maku.express.biza.mapper.orderlist.PayDetailsInfoMapper;
import net.maku.express.biza.pojo.enums.OrderStateEnum;
import net.maku.express.biza.pojo.orderlist.ChargeDetailsInfoPojo;
import net.maku.express.biza.pojo.orderlist.OrderListAnnexsPojo;
import net.maku.express.biza.pojo.orderlist.OrderListPojo;
import net.maku.express.biza.pojo.orderlist.PayDetailsInfoPojo;
import net.maku.express.biza.service.orderlist.OrderListService;
import net.maku.framework.common.exception.ErrorCode;
import net.maku.framework.common.service.impl.BaseServiceImpl;
import net.maku.framework.common.utils.Result;
import net.maku.framework.security.user.SecurityUser;
import net.maku.framework.security.user.UserDetail;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wenyignying'
 * @Description:
 * @Date
 */
@Service
public class OrderListServiceImpl extends BaseServiceImpl<OrderListMapper, OrderListPojo>
        implements OrderListService{

    @Autowired
    private OrderListMapper orderListMapper;

    @Autowired
    private ChargeDetailsInfoMapper chargeDetailsInfoMapper;

    @Autowired
    private PayDetailsInfoMapper payDetailsInfoMapper;

    @Autowired
    private CollectExpressMapper collectExpressMapper;

    @Autowired
    private OrderListAnnexsMapper annexsMapper;
    @Autowired
    private CreateSysNOHandler createSysNOHandler;
    @Autowired
    private CollectExpressSmallMapper collectExpressSmallMapper;

    @Override
    public Result<Integer> add(OrderListVO orderListVO) {
        if (CollectionUtils.isEmpty(orderListVO.getChars())) {
            return Result.error("收费明细不能为空");
        }
        UserDetail user = SecurityUser.getUser();
        OrderListPojo orderListPojo = OrderListConvert.INSTANCE.convert(orderListVO);
        orderListPojo.setOrderNo(createSysNOHandler.createSysNo(4, null));
//        orderListMapper.insertSelective(orderListPojo);
        int num = baseMapper.insert(orderListPojo);
        if (num > 0) {
            List<PayDetailsInfoPojo> list = Convert.toList(PayDetailsInfoPojo.class, orderListVO.getPays());
            List<ChargeDetailsInfoPojo> list1 = Convert.toList(ChargeDetailsInfoPojo.class, orderListVO.getChars());
            payDetailsInfoMapper.insertBatch(list, orderListPojo.getId());
            chargeDetailsInfoMapper.insertBatch(list1, orderListPojo.getId());
            List<OrderListAnnexsPojo> annexsPojos = new ArrayList<>();
            OrderListAnnexsPojo pojo;
            for (OrderListAddAnnexRequestVO ov : orderListVO.getAnnexs()) {
                pojo = new OrderListAnnexsPojo();
                pojo.setAnnex(ov.getAnnex());
                pojo.setType(ov.getType());
                pojo.setOrderListId(orderListPojo.getId());
                pojo.setCreator(user.getId());
                annexsPojos.add(pojo);
            }
            annexsMapper.insertBatch(annexsPojos);
            collectExpressMapper.updateOrderStateAndOriginIdByCollectNo(OrderStateEnum.NO.getCode(), orderListPojo.getOrderNo(), orderListVO.getCollectIds());
            collectExpressSmallMapper.updatePackageCollectIdById1(orderListVO.getCollectIds(), 3);
        }
        return Result.ok();
    }

    @Override
    public Result<Integer> add(List<OrderListVO> orderListVO) {
        for (OrderListVO listAddRequestVO : orderListVO) {
            add(listAddRequestVO);
        }
        return Result.ok();
    }

    @Override
    public Result<Integer> delete(Long id) {
        if (id == null) {
            return Result.error(ErrorCode.NOEMPTY);
        }
        OrderListPojo orderListPojo = orderListMapper.selectById(id);
        if (orderListPojo == null) {
            return Result.error(ErrorCode.NOEMPTY.getCode(), "该数据已被删除！");
        }
        if (orderListPojo.getExamineState() == OrderStateEnum.MIDDLE.getCode()) {
            return Result.error(ErrorCode.NOEMPTY.getCode(), "该数据审核中，无法删除！");
        }
        if (orderListPojo.getExamineState() == OrderStateEnum.SUCCESS.getCode()) {
            return Result.error(ErrorCode.NOEMPTY.getCode(), "该数据审核成功，无法删除！");
        }
        orderListMapper.deleteById(id);
        payDetailsInfoMapper.deleteByOrderId(id);
        chargeDetailsInfoMapper.deleteByOrderId(id);
        collectExpressMapper.updateOrderStateAndOriginIdByOriginId(OrderStateEnum.NO.getCode(), null, orderListPojo.getOrderNo());
        return Result.ok();
    }

    @Override
    public Result<Integer> update(OrderListUpdateRequestVO orderListUpdateRequestVO) {
        OrderListPojo orderListPojo = orderListMapper.selectById(orderListUpdateRequestVO.getId());
        if (orderListPojo == null) {
            return Result.error(ErrorCode.NOEMPTY.getCode(), "该数据已被删除！");
        }
        if (orderListPojo.getExamineState() == OrderStateEnum.MIDDLE.getCode()) {
            return Result.error(ErrorCode.NOEMPTY.getCode(), "该数据审核中，无法修改！");
        }
        if (orderListPojo.getExamineState() == OrderStateEnum.SUCCESS.getCode()) {
            return Result.error(ErrorCode.NOEMPTY.getCode(), "该数据审核成功，无法修改！");
        }
        UserDetail user = SecurityUser.getUser();
        orderListMapper.updateSelective(orderListUpdateRequestVO);
        payDetailsInfoMapper.deleteByOrderId(orderListUpdateRequestVO.getId());
        chargeDetailsInfoMapper.deleteByOrderId(orderListUpdateRequestVO.getId());
        List<PayDetailsInfoPojo> list = Convert.toList(PayDetailsInfoPojo.class, orderListUpdateRequestVO.getPays());
        List<ChargeDetailsInfoPojo> list1 = Convert.toList(ChargeDetailsInfoPojo.class, orderListUpdateRequestVO.getChars());
        payDetailsInfoMapper.insertBatch(list, orderListUpdateRequestVO.getId());
        chargeDetailsInfoMapper.insertBatch(list1, orderListUpdateRequestVO.getId());
        List<String> list2 = new ArrayList<>();
        for (ChargeDetailsInfoPojo ch : list1) {
            list2.add(ch.getCollectNo());
        }
        collectExpressMapper.updateOrderStateAndOriginIdByOriginId(OrderStateEnum.NO.getCode(), null, orderListPojo.getOrderNo());
        collectExpressMapper.updateOrderStateAndOriginIdByCollectNo(OrderStateEnum.NO.getCode(), orderListPojo.getOrderNo(), orderListUpdateRequestVO.getCollectIds());
        List<OrderListAnnexsPojo> annexsPojos = new ArrayList<>();
        OrderListAnnexsPojo pojo = null;
        for (OrderListAddAnnexRequestVO ov : orderListUpdateRequestVO.getAnnexs()) {
            pojo = new OrderListAnnexsPojo();
            pojo.setAnnex(ov.getAnnex());
            pojo.setType(ov.getType());
            pojo.setOrderListId(orderListPojo.getId());
            pojo.setCreator(user.getId());
            annexsPojos.add(pojo);
        }
        annexsMapper.deleteByOrderListId(orderListUpdateRequestVO.getId());
        annexsMapper.insertBatch(annexsPojos);
        return Result.ok();
    }

    @Override
    public Result<Page<OrderListSelectResponseVO>> select(OrderListSelectRequestVO ol) {
        Page<OrderListSelectResponseVO> page = Page.of(ol.getPage(), ol.getPageSize());
        page = orderListMapper.selectAll(page, ol);
        return Result.ok(page);
    }

    /**
     * 单条数据查询
     *
     * @param id
     * @return
     */
    @Override
    public Result<OrderListSelectOneResponseVO> selectOne(Long id) {
        OrderListSelectOneResponseVO ov1 = orderListMapper.selectById1(id);
        if (ov1 == null) {
            return Result.error(ErrorCode.NOEMPTY.getCode(), "数据不存在");
        }
        Result result = Result.ok(ov1);
        return result;
    }

    @Override
    public Result<List<PayDetailResponseVO>> selectPayOne(Long id) {
        return Result.ok(payDetailsInfoMapper.selectByOrderId(id));
    }

    @Override
    public Result<List<ChargeAddResponseVO>> selectChargeOne(Long id) {
        return Result.ok(chargeDetailsInfoMapper.selectByOrderId(id));
    }

    @Override
    public List<OrderPackingListVO> selectOrderPacking() {
        return orderListMapper.selectOrderPacking();
    }


    public List export(JSONObject jsonObject) {
        ArrayList<List> orderList = new ArrayList<>();



        return orderList;
    }
}
